﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using common;
using dao;
using dto;

namespace GTVAccountingSystem.view.form
{
    public partial class frmDetailUser : Form
    {
        UsersDAO userDAO = new UsersDAO();
        frmMain main;
        private String firstNameText = "";
        //private String lastNameText = "";
        private String emailText = "";
        private String addressText = "";
        private String phone1Text = "";
        private String phone2Text = "";
        private String birthDay = "";
        private String idNumber = "";
        private String faxText = "";
        String error = "";
        int index;
        public frmDetailUser(frmMain main, int index)
        {
            this.main = main;
            this.index = index;
            InitializeComponent();
            loadData();
            setLanguage();
        }
        /* Author : Jenny
          * Date : 10/4/2012
          * function setLanguage
          * set language
          * no return
          */
        public void setLanguage()
        {
            lblUsername.Text = Constant.RM.GetString("lblUsername", Constant.CULTURE) + " : ";
            lblStaffCode.Text = Constant.RM.GetString("lblStaffCode", Constant.CULTURE) + " : ";
            lblFullName.Text = Constant.RM.GetString("lblFullName", Constant.CULTURE) + " : ";
            lblEmail.Text = Constant.RM.GetString("lblEmail", Constant.CULTURE) + " : ";
            lblAddress.Text = Constant.RM.GetString("lblAddress", Constant.CULTURE) + " : ";
            lblPhone1.Text = Constant.RM.GetString("lblPhone", Constant.CULTURE) + " 1 : ";
            lblPhone2.Text = Constant.RM.GetString("lblPhone", Constant.CULTURE) + " 2 : ";
            lblFax.Text = Constant.RM.GetString("lblFax", Constant.CULTURE) + " : ";
            lblBirthday.Text = Constant.RM.GetString("lblBirthday", Constant.CULTURE) + " : ";
            lblIdNumber.Text = Constant.RM.GetString("lblIDNumber", Constant.CULTURE) + " : ";
            btnUpdate.Text = Constant.TAB + Constant.RM.GetString("btnUpdate", Constant.CULTURE);
            btnClear.Text = Constant.TAB + Constant.RM.GetString("btnClear", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmDetailUser", Constant.CULTURE);
        }
        /* Author : Jenny
          * Date : 3/4/2012
          * function btnClear_Click
          * Clear all data in this form
          * no return
          */
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtFullName.Text = "";
            //txtLastName.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtPhone1.Text = "";
            txtPhone2.Text = "";
            txtFax.Text = "";
            txtIDNumber.Text = "";
            dtpBirthday.Text = "";
            disableErrorLable();
        }
        /* Author : Jenny
          * Date : 3/4/2012
          * function disableErrorLable
          * hide the error lable when start
          * no return
          */
        public void disableErrorLable()
        {
            txtFullName.BackColor = SystemColors.Window;
            //txtLastName.BackColor = SystemColors.Window;
            txtAddress.BackColor = SystemColors.Window;
            txtEmail.BackColor = SystemColors.Window;
            txtPhone1.BackColor = SystemColors.Window;
            txtPhone2.BackColor = SystemColors.Window;
            txtFax.BackColor = SystemColors.Window;
            txtIDNumber.BackColor = SystemColors.Window;
            
        }
        /* Author : Jenny
        * Date : 3/4/2012
        * function validateUser
        * validate data before save
        * return Boolean
        */
        public void validateUser()
        {
            error = "";
            disableErrorLable();
            firstNameText = txtFullName.Text.Trim();
            //lastNameText = txtLastName.Text.Trim();
            emailText = txtEmail.Text.Trim();
            addressText = txtAddress.Text.Trim();
            phone1Text = txtPhone1.Text.Trim();
            phone2Text = txtPhone2.Text.Trim();
            faxText = txtFax.Text.Trim();
            idNumber = txtIDNumber.Text.Trim();
            if (firstNameText.Equals(""))
            {
                txtFullName.BackColor = Color.MistyRose;
                error += "Input First Name\n";
            }
            /*if (lastNameText.Equals(""))
            {
                txtLastName.BackColor = Color.MistyRose;
                error += "Input Last Name\n";
            }*/
            if (addressText.Equals(""))
            {
                txtAddress.BackColor = Color.MistyRose;
                error += "Input Address\n";
            }
            if (emailText.Equals(""))
            {
                txtEmail.BackColor = Color.MistyRose;
                error += "Input Email\n";
            }
            else
            {
                if (!userDAO.checkEmail(emailText))
                {
                    txtEmail.BackColor = Color.MistyRose;
                    error += "Email is not valid\n";
                }
            }
            if (idNumber.Equals(""))
            {
                txtIDNumber.BackColor = Color.MistyRose;
                error += "Input ID Number\n";
            }
            if (phone1Text.Equals(""))
            {
                txtPhone1.BackColor = Color.MistyRose;
                error += "Input Phone 1\n";
            }
            else
            {
                if (!userDAO.checkPhoneNumber(phone1Text))
                {
                    txtPhone1.BackColor = Color.MistyRose;
                    error += "Phone 1 must be number\n";
                }
                else if (!(8 <= phone1Text.Length && phone1Text.Length <= 15))
                {
                    txtPhone1.BackColor = Color.MistyRose;
                    error += "The length of phone 1 is not valid ( 8 - 15 )\n";
                }
            }
            if (!phone2Text.Equals(""))
            {
                if (!userDAO.checkPhoneNumber(phone2Text))
                {
                    txtPhone2.BackColor = Color.MistyRose;
                    error += "Phone 2 must be number\n";
                }
                else if (!(8 <= phone2Text.Length && phone2Text.Length <= 15))
                {
                    txtPhone2.BackColor = Color.MistyRose;
                    error += "The length of phone 2 is not valid ( 8 - 15 )\n";
                }
            }
            
        }
        /* Author : Jenny
        * Date : 3/4/2012
        * function btnUpdate_Click
        * validate data before save
        * return Boolean
        */
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            validateUser();
            if (error.Equals(""))
            {
                String contentLog = "";
                UserInformationsDTO userInformationOld = Constant.USER_INFORMATIONDTO;
                UserInformationsDTO userInformation = new UserInformationsDTO();
                userInformation.FirstName = firstNameText;
                //userInformation.LastName = lastNameText;
                userInformation.Address = addressText;
                userInformation.Email = emailText;
                userInformation.Phone1 = phone1Text;
                userInformation.Phone2 = phone2Text;
                userInformation.Birth = dtpBirthday.Value.ToShortDateString();
                userInformation.IdNumber = idNumber;
                userInformation.Fax = faxText;
                if (!userInformationOld.FirstName.Equals(userInformation.FirstName))
                    contentLog += "change Full Name : " + userInformationOld.FirstName + " to " + userInformation.FirstName + "\n";
                /*if (!userInformationOld.LastName.Equals(userInformation.LastName))
                    contentLog += "change Last Name : " + userInformationOld.FirstName + " to " + userInformation.FirstName + "\n";*/
                if (!userInformationOld.Birth.Equals(userInformation.Birth))
                    contentLog += "change Address : " + userInformationOld.Birth + " to " + userInformation.Birth + "\n";
                if (!userInformationOld.Address.Equals(userInformation.Address))
                    contentLog += "change Address : " + userInformationOld.Address + " to " + userInformation.Address + "\n";
                if (!userInformationOld.Email.Equals(userInformation.Email))
                    contentLog += "change Email : " + userInformationOld.Email + " to " + userInformation.Email + "\n";
                if (!userInformationOld.IdNumber.Equals(userInformation.IdNumber))
                    contentLog += "change ID Number : " + userInformationOld.IdNumber + " to " + userInformation.IdNumber + "\n";
                if (!userInformationOld.Phone1.Equals(userInformation.Phone1))
                    contentLog += "change Phone1 : " + userInformationOld.Phone1 + " to " + userInformation.Phone1 + "\n";
                if (!userInformationOld.Phone2.Equals(userInformation.Phone2))
                    contentLog += "change Phone2 : " + userInformationOld.Phone2 + " to " + userInformation.Phone2;
                if (!userInformationOld.Fax.Equals(userInformation.Fax))
                    contentLog += "change fax : " + userInformationOld.Fax + " to " + userInformation.Fax + "\n";
                userInformation.Id = userInformationOld.Id;
                userInformation.Birth = userDAO.sortDate(userInformation.Birth);
                if (userDAO.updateUserInformations(userInformation, Constant.DR_ACTIONS_VARIABLE["varUpdate"].ToString(), contentLog))
                {
                    MessageBox.Show("Update succesful!!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                
            }
            else
            {
                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        /* Author : Jenny
        * Date : 3/4/2012
        * function frmDetailUser_Load
        * load data to textfields of form
        * return Boolean
        */
        public void loadData()
        {
            UserInformationsDTO userInformation = Constant.USER_INFORMATIONDTO;
            txtFullName.Text = userInformation.FirstName;
            //txtLastName.Text = userInformation.LastName;
            txtUsername.Text = Constant.USER_LOGIN.Username;
            txtStaffCode.Text = userInformation.StaffCode;
            txtAddress.Text = userInformation.Address;
            txtEmail.Text = userInformation.Email;
            txtPhone1.Text = userInformation.Phone1;
            txtPhone2.Text = userInformation.Phone2;
            txtIDNumber.Text = userInformation.IdNumber;
            txtFax.Text = userInformation.Fax;
            try
            {
                dtpBirthday.Value = Convert.ToDateTime(userInformation.Birth);
            }
            catch (Exception ex) { }
        }

        private void frmDetailUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            //main.Enabled = true;
            main.listOpen.RemoveAt(index);
        }      
    }
}
